//
//  HQYGetLocationNetworkingManager.m
//  Go!
//
//  Created by rimi on 16/7/8.
//  Copyright © 2016年 黄秋燕. All rights reserved.
//

#import "HQYGetLocationNetworkingManager.h"
#import "AFNetworking/AFNetworking.h"

@implementation HQYGetLocationNetworkingManager

+ (void)postWithParams:(NSDictionary *)params
          Successblock:(Success)successBlock
             Failblock:(Failure)failureBlock {
    NSInteger networkType = [self checkNetWork];
    if (networkType == 0) {
        [Tools showAlertWithMessage:@"网络连接失败,请检查网络"];
    }
    else {
        NSString *completeUrl = @"http://route.showapi.com/238-2";
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        
        NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:params];
        [dic setObject:@"18651" forKey:@"showapi_appid"];
        [dic setObject:@"faca3dd01cc74c9ca6619a9f54495159" forKey:@"showapi_sign"];
        // 网络请求
        [manager POST:completeUrl parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            if (successBlock) {
                // 向外部传值
                successBlock(responseObject);
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            if (failureBlock) {
                // 失败向外部传递失败信息
                failureBlock(error);
            }
        }];
    }
}


#pragma mark - 网络判断
+ (NSInteger)checkNetWork {
    // 状态栏是由当前控制器控制的，首先获取当前app。
    UIApplication *app = [UIApplication sharedApplication];
    
    // 遍历状态栏上的前景视图
    NSArray *children = [[[app valueForKeyPath:@"statusBar"] valueForKeyPath:@"foregroundView"] subviews];
    
    int type = 0;
    
    for (id child in children) {
        if ([child isKindOfClass:NSClassFromString(@"UIStatusBarDataNetworkItemView")]) {
            type = [[child valueForKeyPath:@"dataNetworkType"] intValue];
        }
    }
    // type数字对应的网络状态依次是：0：无网络；1：2G网络；2：3G网络；3：4G网络；5：WIFI信号
    //    NSLog(@"type is '%d'.", type);
    return type;
}
@end
